package mrusanov.nation

import mrusanov.player.PlayerType
import mrusanov.team.Club
import util.Random
import mrusanov.league.LeagueInfoSystem
import mrusanov.localization.Localization


class Nation(val nameKey:String, val clubs:List[Club], private var _popularity:Int = 0,
             val availablePlayerTypes:List[PlayerType], val leagueInfoSystem:LeagueInfoSystem) {
  private val minPopularity = 0
  private val maxPopularity = 100

  def popularity = _popularity

  def name = Localization(nameKey)

  def popularity_=(value:Int) {
    _popularity = if (value < minPopularity) minPopularity
                  else if (value > maxPopularity) maxPopularity
                  else value
  }

  // TODO implement this
  // maximum should be 100 000 for two very best clubs
  private def calculateBaseAttendance(host:Club, guest:Club, world:World):Double = {
    val totalClubs = world.clubRatingList.size
    val hostPosition = world.clubRatingMap(host) / totalClubs.toDouble
    val guestPosition = world.clubRatingMap(guest) / totalClubs.toDouble
    val avg = 1 - (hostPosition + guestPosition) / 2
    avg * 100000
  }

  def attendance(host:Club, guest:Club, world:World):Int = {
    val base = calculateBaseAttendance(host, guest, world)
    (base * popularity / maxPopularity.toDouble).round.toInt
  }

  clubs foreach(_.nation = this)

  def randomPlayerType = availablePlayerTypes(Random.nextInt(availablePlayerTypes.size))

  override def toString = name
}
